<template>
  <div class="tree">
    <el-tree
      ref="treeone"
      :data="treeData"
      :default-expanded-keys="[2,3]"
      node-key="id"
      :props="defaultProps"
      :check-strictly="true"
      :highlight-current="true"
      :expand-on-click-node="false"
      @node-click="handleNodeClick"
    />
  </div>
</template>

<script>
import { fetchTree } from '@/api/sys'
import { treeDataTranslate } from '@/utils'
export default {
  data() {
    return {
      historyId: '',
      treeData: [],
      defaultExpandedKeys: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  computed: {
    userInfo: {
      get() {
        return this.$store.state.user.userInfo
      }
    }
  },
  created() {
    this.getTree()
  },
  mounted() {

  },
  methods: {
    handleNodeClick(data) {
      // console.log(this.$refs.tree.getCurrentKey());
      // console.log("------------",data)
      this.$emit('set-tree-data', data)
    },
    currentClick(id) {
      this.historyId = id
      this.$nextTick(() => {
        this.$refs['treeone'].setCurrentKey(id)
      })
    },
    async getTree() {
      const parameter = {
        deptType: 1
      }
      // let parentCode = this.userInfo.departCode //this.userInfo.departCode.substring(0, this.userInfo.departCode.lastIndexOf("-"))
      // if (this.userInfo.dataAuth === "self") {
      //     pamData["manageName"] = parentCode
      // }
      const { data } = fetchTree(parameter)

      data.forEach(el => {
        el.nameAndManage = el.manage == '' || !el.manage ? el.name : `${el.name} - ${el.manage}`
      })

      this.treeData = treeDataTranslate(res, 'code', 'parentCode')

      this.$nextTick(() => {
        this.$refs['treeone'].setCurrentKey(this.historyId)
      })
    }

  }
}
</script>

<style>
.tree {
    width: 100%;
    height: 100%;
    overflow-y: auto;
}
</style>
